#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e9;
const int M = 1e9 + 7;

int t, n, m;

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0);

    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        if (n > m)
        {
            cout << 0 << endl;
            continue;
        }

        int k = 0, t = m + 1;
        for (int i = 30; i >= 0; i--)
        {
            int ni = n >> i & 1, ti = t >> i & 1;
            if (ni == 1 && ti == 0)
                break;
            if (ni == 0 && ti == 1)
                k |= 1 << i;
        }
        cout << k << endl;
    }
    return 0;
}  